//
//  XQCourseViewController.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/11/24.
//  Copyright © 2016年 校企桥. All rights reserved.
//
import Alamofire
import ObjectMapper
import AlamofireObjectMapper
import SwiftyJSON
import UIKit

class XQCourseViewController: XQBaseViewController,UITableViewDelegate,UITableViewDataSource {
    private let XQHomeFiveTableViewCellID = "XQHomeFiveTableViewCellID"
    private let XQCourseFirstTableViewCellID = "XQCourseFirstTableViewCellID"
    private let XQCourseSecondTableViewCellID = "XQCourseSecondTableViewCellID"

    
    private  var bannerView: LLCycleScrollView!
    private  var tableView = UITableView()

    
    private  var urlArray = NSMutableArray()
    private  var imgurlArray = NSMutableArray()
    
    private  var courseMode:XQCourseMode?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "课程"
        // Do any additional setup after loading the view.
        self.navigationItem.leftBarButtonItem = nil
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
  private  func InitlunBoView(){
        self.bannerView = LLCycleScrollView.llCycleScrollViewWithFrame(CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 150))
        bannerView.scrollDirection = .horizontal
        bannerView.autoScrollTimeInterval = 3.0
        bannerView.placeHolderImage = UIImage.init(named: "homeheader")
        bannerView.coverImage = UIImage.init(named: "homeheader")
        
    }
    
    override func setRootView() {
        
        InitlunBoView()
        
        self.tableView = UITableView.init(frame:  CGRect.zero, style: .grouped)
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.showsVerticalScrollIndicator = false
        self.view.addSubview(self.tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.left.equalTo(self.view.snp.left)
            make.right.equalTo(self.view.snp.right)
            make.top.equalTo(self.view.snp.top)
            make.bottom.equalTo(self.view.snp.bottom)
        }
        
        self.tableView.tableHeaderView = bannerView
        
        self.tableView .register(XQHomeFiveTableViewCell.self, forCellReuseIdentifier: XQHomeFiveTableViewCellID)

        self.tableView.register(XQCourseFirstTableViewCell.self, forCellReuseIdentifier: XQCourseFirstTableViewCellID)
        self.tableView.register(XQCourseSecondTableViewCell.self, forCellReuseIdentifier: XQCourseSecondTableViewCellID)
        
        courseBanenrData()
        courseContentData()
        
        
    }
    

    private func courseBanenrData() {
        
        let urlstr = MTBaseURL.appending("NewVerCourse/index.json")
    
        Alamofire.request(urlstr).responseJSON { (response) in
        
            self.urlArray = NSMutableArray.init()
            self.imgurlArray = NSMutableArray.init()
            
            let result = JSON.init(data: response.data!)
        
            let mainArray = result["main"].arrayValue;
        
            for subJson:JSON in mainArray {
            //Do something you want
            //NSLog("++++%@", subJson["img"].stringValue)
            //NSLog("----%@", subJson["uri"].stringValue)
                let img = subJson["img"].stringValue
                let uri = subJson["uri"].stringValue
            
                self.urlArray .add(uri)
                self.imgurlArray .add(img)
            
        }
        
            let imgurl:[NSString] = NSArray.init(array: self.imgurlArray) as! [NSString];
        
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
            self.bannerView.imagePaths = imgurl as Array<String>
        }
    }
}



      private func courseContentData() {
    
        let urlstr = MTBaseURL.appending("NewVerCourse/courseRecommend.json")
    
        Alamofire.request(urlstr).responseObject { (response:DataResponse<XQCourseMode>) in
            print(response)
            self.courseMode = response.result.value
            self.tableView.reloadData()
        }
    
}



    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 2 {
        
            if ((courseMode?.free) != nil) {
                return (courseMode?.free?.count)!
            }else{
                return 0
            }
            
        }else{
            return 1
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        var sectionHeight = CGFloat.init(0)
        switch section {
        case 0:
            sectionHeight = 0.1
            break
        case 1:
            sectionHeight = 30
            break
        case 2:
            sectionHeight = 30
            break
        default:
            break
        }
        return sectionHeight
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 1 {
            return "名师讲座"
        }else if section == 2{
            return "猜你喜欢"
        }else{
        return ""
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 40
        }else if indexPath.section == 1 {
            return 100
        }else{
            return 80
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let fitstCell = tableView.dequeueReusableCell(withIdentifier: XQCourseFirstTableViewCellID) as! XQCourseFirstTableViewCell
            return fitstCell
            
        }else if indexPath.section == 1{
            let secondCell = tableView.dequeueReusableCell(withIdentifier: XQCourseSecondTableViewCellID) as!XQCourseSecondTableViewCell
            if ((courseMode?.paid) != nil) {
                secondCell.dataArray = (courseMode?.paid)!
                secondCell.collectionView .reloadData()
            }
            secondCell.courseself = self
            return secondCell
            
        }else{
            let courseCell = tableView.dequeueReusableCell(withIdentifier: XQHomeFiveTableViewCellID) as!XQHomeFiveTableViewCell
            if ((courseMode?.free) != nil) {
                courseCell.courseMode = courseMode?.free?[indexPath.row]
            }
            return courseCell
        }
        
        }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let  detailVc =  XQCourseDetailViewController()
        let courseMode = self.courseMode?.free?[indexPath.row]
        detailVc.cid = (courseMode?.id)!
        detailVc.type = (courseMode?.type)!
        detailVc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(detailVc, animated: true)
    }
    
}

